import java.util.*;

class A{
    public void func1(){

    }
}
class B extends A{

}
public class Example{
    String str = new String("good");
    char[]ch = {'a','b','c'};
    public static void main1(String args[]){
        Example ex=new Example();
        ex.change(ex.str,ex.ch);
        System.out.print(ex.str+" and ");
        System.out.print(ex.ch);
    }
    public static void main(String[] args) {
        System.out.println(reverseWords("a good   example"));
        B b = new B();
        b.func1();
        Stack<Object> stack = new Stack<>();
        stack.isEmpty();
    }
    public static String reverseWords(String s) {
        // 移除所有的空格
        StringBuilder sb = removeSpace(s);
        // 翻转整个字符串
        reverse(sb,0,sb.length() - 1);
        // 翻转每个单词并删除多余空格
        StringBuilder ret = new StringBuilder();
        int start = 0;
        int end = 0;
        for (; end < sb.length(); end++) {
            if (sb.charAt(end) == ' '){
                String temp = sb.substring(start,end);
                ret.append(reverse(new StringBuilder(temp),0,temp.length() - 1) + " ");
                while (sb.charAt(end) == ' ') {
                    end++;
                }
                start = end;
            }
        }
        String temp = sb.substring(start,end);
        ret.append(reverse(new StringBuilder(temp),0,temp.length() - 1));
        return ret.toString();
    }
    // 移除两端的空格
    public static StringBuilder removeSpace(String s) {
        int start = 0;
        int end = s.length();
        while(start < s.length() && s.charAt(start) == ' ') start++;
        while(end > 0 && s.charAt(end - 1) == ' ') end--;
        return new StringBuilder(s.substring(start, end));
    }
    // 逆置字符串  [begin,end]
    public static StringBuilder reverse(StringBuilder sb, int begin, int end) {
        while(begin < end) {
            char temp = sb.charAt(begin);
            sb.setCharAt(begin, sb.charAt(end));
            sb.setCharAt(end, temp);
            begin++;
            end--;
        }
        return sb;
    }




    public static void main5(String[] args) {
        Object o = new Object();
        System.out.println(Math.round(11.5));

        int[] nums1 = {};
        int[] nums2 = {};
        intersect(nums1,nums2);

        HashSet set = new HashSet();

    }
    public static int[] intersect(int[] nums1, int[] nums2) {
        int[] ret = new int[Math.min(nums1.length,nums2.length)];

        Map<Integer,Integer> map = new HashMap<>();
        for (int num : nums1) {
            int counts = map.getOrDefault(num,0);
            map.put(num,counts + 1);
        }

        int index = 0;
        for (int num : nums2) {
            if(map.containsKey(num)) {
                if (map.get(num) > 0) {
                    ret[index++] = num;
                    map.put(num,map.get(num)-1);
                }
            }
        }
        return ret;
    }


    public static void main4(String[] args) {
        List<Integer> anagrams = findAnagrams("cbaebabacd", "abc");
        System.out.println(anagrams.toString());
    }
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();
        int[] flags = new int[26];
        for (char c : p.toCharArray()) {
            flags[c - 'a']++;
        }// 设置标志
        int len = p.length();
        char[] chars = s.toCharArray();

        for(int i = 0; i < chars.length; i++) {
            if (flags[chars[i] - 'a'] != 0) {
                int[] temp = Arrays.copyOf(flags,26);
                boolean flag = true;
                for (int j = i; j < len + i; j++) {
                    if (j >= 26) {
                        flag = false;
                        break;
                    }
                    temp[chars[j] - 'a']--;
                    if (temp[chars[j] - 'a'] < 0) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    ret.add(i);
                }
            }
        }
        return ret;
    }


    public static void main2(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate"};
        System.out.println(groupAnagrams(strs));
    }
    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> ret = new ArrayList<>();
        HashMap<String,List<String>> map = new HashMap<>();

        for (String s : strs) {
            char[] chars = s.toCharArray();
            int[] flags = new int[26];// 标识每个单词中字母组成情况
            for (char aChar : chars) {
                flags[aChar - 'a']++;
            }
            String str = Arrays.toString(flags);
            if (map.containsKey(str)) {
                map.get(str).add(s);
            } else {
                map.put(str, new ArrayList<>());
                map.get(str).add(s);
            }
        }
        ret.addAll(map.values());
        return ret;
    }

    public void change(String str,char ch[]){
    //引用类型变量，传递的是地址，属于引用传递。
        str += "ha";
        ch[0]='g';
    }
}
